﻿package com.winglish.as3.loader
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	import flash.system.Capabilities;
	import flash.system.ApplicationDomain;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.events.EventDispatcher;
	
	public class ImgLoader extends EventDispatcher{

		private var _loader: Loader;
		private var _context: LoaderContext;
		private var _imgUrl: String;
		private var _key: String;
		private var _request: URLRequest;
		private var _complete: Function;
		private var _progress: Function;
		private var _isOnce: Boolean;
		private var _isStart: Boolean;
		private var _isLock: Boolean;
		
		
		/**
		 * ImgLoader
		 * @param	url
		 * @param	obj
		 */
		public function ImgLoader( url: String = null, obj: Object = null )
		{
			_imgUrl = url;		
			if( obj ){
				if( obj.key ) _key = obj.key;
				if( obj.complete ) _complete = obj.complete;
				if( obj.progress ) _progress = obj.progress;
				if( !obj.isOnce ) _isOnce = true;
				if( !obj.isStart ) _isStart = true;
			}
			if( _isStart ) load( _imgUrl );
		}

		/**
		 * load
		 * @param	url
		 */
		public function load( url: String = null ): void{
			if ( _isLock ) return;
			else{
				_context = new LoaderContext();
				_context.checkPolicyFile = true;
				_imgUrl = url;	
				_isLock = true;
				_request = new URLRequest( url );
				_loader = new Loader();
				setEvent();		
				_loader.load( this._request, _context );				
			}
		}
		
		/**
		 * unload
		 */
		public function unload(): void
		{
			setEvent( false );
			stop();
			try{
				var li:LoaderInfo = _loader.contentLoaderInfo;
				if ( li.childAllowsParent && li.content is Bitmap )(li.content as Bitmap).bitmapData.dispose();
				_loader.unload();
			}catch( e:Error ){
				throw new Error( "!!" );
			}
		}
		public function stop(): void
		{
			try{
				_loader.close();
				_isLock = false;
			}catch( e:Error ){
				throw new Error( "not Close!!" );
			}
			_loader.unload(); 
			onCompleteHnadler( null );
		}
		
		public function getData( obj: Object = null ): Object{
			return _loader.content;
		}
		
		private function setEvent( isEvent: Boolean = true ): void{
			var listener: String = ( isEvent ) ?"addEventListener" : "removeEventListener";
			_loader.contentLoaderInfo[ listener ]( Event.COMPLETE, onCompleteHnadler );
			_loader.contentLoaderInfo[ listener ]( IOErrorEvent.IO_ERROR, onIoErrorHandler );
			_loader.contentLoaderInfo[ listener ]( ProgressEvent.PROGRESS, onProgressHandler );
		}

		private function onCompleteHnadler( e: Event ): void{
			_isLock = false;
			if( _complete != null ) _complete( getData( e.target ), _key );
			if( _isOnce ) setEvent( false );
			if( e != null ) dispatchEvent( new Event( Event.COMPLETE ) );
		}
		
		private function onIoErrorHandler( e: IOErrorEvent ): void{
			trace("img error : " + e.text );
			_isLock = false;
		}
		
		private function onProgressHandler( e: ProgressEvent ): void{
			if( _progress != null ){
				if ( e.target.bytesTotal > 0 )_progress( e.target.bytesLoaded, e.target.bytesTotal );
			}
		}
		
		public function get getUrl():String {
			return _imgUrl;
		}
	}
}